package org.csu.goodpartner.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.goodpartner.VO.ItemVO;
import org.csu.goodpartner.common.CommonResponse;
import org.csu.goodpartner.entity.Item;
import org.csu.goodpartner.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 商品业务：
 1、发布商品
 2、更改商品信息
 3、下架商品
 4、查看商品
 */
@CrossOrigin
@Controller
public class ItemController {
    @Autowired
    private ItemService itemService;

    //发布商品 /shop/{shopId}/publish
    @ResponseBody
    @PostMapping("shop/{shopId}/publish")
    public CommonResponse publish(@RequestBody ItemVO itemVO){

        return itemService.publish(itemVO);
    }

    //更改商品信息 /shop/{shopId}/update
    @ResponseBody
    @PostMapping("shop/{shopId}/update")
    public CommonResponse update(@RequestBody Map map){
        try{
            System.out.println(map.get("itemid"));
            int itemid = (int) map.get("itemid");
            String name = (String)map.get("name");
            int shopid = Integer.parseInt((String) map.get("shopid"));
            String classone = (String)map.get("classone");
            String classtwo = (String)map.get("classtwo");
            BigDecimal listprice = new BigDecimal(0).valueOf((int)map.get("listprice"));
            System.out.println(listprice);
            int quantity = (int) map.get("quantity");

            ItemVO itemVO = new ItemVO();
            itemVO.setItemid(itemid);
            itemVO.setName(name);
            itemVO.setCategory("123");
            itemVO.setDescription("123");
            itemVO.setShopid(shopid);
            itemVO.setClassone(classone);
            itemVO.setClasstwo(classtwo);
            itemVO.setListprice(listprice);
            itemVO.setImg("123");
            itemVO.setStatus(0);
            itemVO.setQuantity(123);

            System.out.println(map);
            return itemService.update(itemVO);

        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }

    //下架商品 /shop/{shopId}/out
    @ResponseBody
    @PostMapping("shop/{shopId}/out/{itemId}")
    public CommonResponse out(@PathVariable int shopId, @PathVariable int itemId){
        try{
            return itemService.out(shopId,itemId);
        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }

//    //查看商品 /shop/{shopId}
//    @ResponseBody
//    @PostMapping("shop/{shopId}")
//    public CommonResponse getAllItems(@PathVariable int shopId){
//        try{
//            return itemService.getAllItems(shopId);
//        }catch(Exception e){
//            e.printStackTrace();
//            return CommonResponse.createForError("出现错误，请刷新重试");
//        }
//    }

    //模糊查询 /shop/{shopId}/select
    /*
    @ResponseBody
    @GetMapping("/{shopId}/select")
    public CommonResponse out(@PathVariable int shopId, @RequestParam(value = "like") String like){
        try{
            return null;
        }catch(Exception e){
            e.printStackTrace();
            return CommonResponse.createForError("出现错误，请刷新重试");
        }
    }
    */
    @PostMapping("/shop/{shopId}/select")
    @ResponseBody
    public CommonResponse search(@PathVariable int shopId,@RequestBody Map map)
    {
//        if(s=="")
//            return itemService.getAllItems(shopId);
        return itemService.like(shopId,(String)map.get("params"));
    }
}
